<?php
class OPPowerBar {
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Attributes
	*
	* -------------------------------------------------------------------------
	*/

	/**
	* Goal of the power bar
	*
	* @var int
	*/
	protected $goal;
	
	/**
	* Number of levels of the power bar
	*
	* @var int
	*/
	protected $numberOfLevels;
	
	/**
	* Actual score of the power bar
	*
	* @var int
	*/
	protected $score;
	
	/**
	* Actual level of the power bar
	*
	* @var int
	*/
	protected $level;
	
	/**
	* Array of the Events
	*
	* @var array<OPEvent>
	*/
	protected $events;
	
	/**
	 * -------------------------------------------------------------------------
	 * 
	 * 									Static
	 * 
	 * -------------------------------------------------------------------------
	 */

	/**
	* Static constructor creating a new Power Bar with a goal
	*
	* @param $goal
	* @param $numberOfLevels
	* @return OPPowerBar
	*/
	public static function newWithGoalAndNumberOfLevels($goal, $numberOfLevels){
		$newPowerBar = new OPPowerBar();
		$newPowerBar->setGoal($goal);
		$newPowerBar->setNumberOfLevels($numberOfLevels);
	
		return $newPowerBar;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Dynamic
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Constructor initialise the actual score to 0
	 * 
	 */
	public function OPPowerBar(){
		$this->setScore(0);
		$this->setLevel(1);
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Setters
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Set the goal of the powerBar
	*
	* @param $goal
	* @return OPPowerBar
	*/
	public function setGoal($goal) {
		$this->goal = $goal;
	
		return $this;
	
	}
	
	/**
	* Set the number of levels of the powerBar
	*
	* @param $numberOfLevels
	* @return OPPowerBar
	*/
	public function setNumberOfLevels($numberOfLevels) {
		$this->numberOfLevels = $numberOfLevels;
	
		return $this;
	
	}
	
	/**
	* Set the actual score of the powerBar
	*
	* @param $score
	* @return OPPowerBar
	*/
	public function setScore($score) {
		$this->score = $score;
	
		return $this;
	
	}
	
	/**
	* Set the actual level of the powerBar
	*
	* @param $level
	* @return OPPowerBar
	*/
	public function setLevel($level) {
		$this->level = $level;
	
		return $this;
	
	}
	
	/**
	 * Add an event to events list
	 * 
	 * @param $event
	 * @return OPPowerBar
	 */
	public function addEvent($event) {
		$this->events[] = $event;
		
	}
	
	
	/**
	* -------------------------------------------------------------------------
	* 									Getters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return the goal of the power bar
	 *
	 * @return int
	 */
	public function goal() {
		return $this->goal;
	
	}
	
	/**
	* Return the number of levels of the power bar
	*
	* @return int
	*/
	public function numberOfLevels() {
		return $this->numberOfLevels;
	
	}
	
	/**
	* Return the actual score of the power bar
	*
	* @return int
	*/
	public function score() {
		if (0 > $this->score) {
			return 0;
			
		}
		
		return $this->score;
	
	}
	
	/**
	* Return the actual level of the power bar
	*
	* @return int
	*/
	public function level() {
		return $this->level;
	
	}
	
	/**
	* Return the goal to reach the level of the power bar
	*
	* @return int
	*/
	public function goalToNextLevel() {
		$pointsByLevel = $this->goal / $this->numberOfLevels;
		
		return $pointsByLevel * $this->level;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Score Management
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Increase the actual score by $aNumber
	*
	* @param $aNumber
	* @return false or an OPEvent
	*/
	public function increaseBy($aNumber) {
		$this->score += $aNumber;
		
		return $this->checkActualLevel();
			
	}
	
	/**
	* Decrease the actual score by $aNumber
	*
	* @param $aNumber
	* @return this
	*/
	public function decreaseBy($aNumber) {
		$this->score -= $aNumber;
	
		return $this;
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Level Management
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Check if the actual level corresponds to the actual score
	 * 
	 * @return an OPEvent or false
	 */
	public function checkActualLevel() {
		if ($this->goalToNextLevel() <= $this->score()) {
			$actualLevel = $this->level();
			
			$this->goToNextLevel();
			
			return $this->event($actualLevel);
			
		}
		
		return false;
		
	}
	
	/**
	* Go to next level
	*
	* @return OPPowerBar
	*/
	public function goToNextLevel() {
		$this->setLevel($this->level() + 1);
	
		return $this;
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Event Management
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return the event corresponding to the actual level
	 * 
	 * @param $actualLevel
	 * @return OPEvent
	 */
	public function event($actualLevel) {
		return $this->events[$actualLevel - 1];
		
	}
	
}